package com.banzhi.shop.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.banzhi.shop.R;
import com.banzhi.shop.adapter.CommonAdapter;
import com.banzhi.shop.data.PartnerProfitBean;
import com.banzhi.shop.entity.shouyijilu.Shouyi_Data;
import com.banzhi.shop.model.CallBack.PartnerShouyejiluCallBack;

import com.banzhi.shop.model.Impl.PartnerShouyijiluImpl;

import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.DateUtil;
import com.banzhi.shop.utils.EndlessRecyclerOnScrollListener;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.NumberUtils;
import com.banzhi.shop.utils.ShareUtils;
import com.banzhi.shop.utils.ToastUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class PartnerShouyi_DaifaFragment extends Fragment implements PartnerShouyejiluCallBack, SwipeRefreshLayout.OnRefreshListener {
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView recyclerView;

    private int page = 1;
 //   private List<Shouyi_Data> shouyidata = new ArrayList<>();
    private CommonAdapter<Shouyi_Data> adapter;
    private LinearLayoutManager LayoutManager;
    private RelativeLayout rlnodata;
    private View view;
    private PartnerShouyijiluImpl model;
    private List<PartnerProfitBean> mBeans;
    private List<PartnerProfitBean> mPartnerProfitBeans = new ArrayList<>();
    private CommonAdapter<PartnerProfitBean> mAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_shouyi__daifa, container, false);
        setinits(view);
        setlisteners();

        model = new PartnerShouyijiluImpl();

        LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
        model.getData(page, "0", this);
        return view;
    }

    public void setinits(View view) {
        refreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.srl_listview);
        refreshLayout.setColorSchemeResources(R.color.themeColor);
        refreshLayout.setProgressBackgroundColorSchemeResource(R.color.colorWhite);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        LayoutManager = new LinearLayoutManager(getActivity());
        LayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(LayoutManager);
        rlnodata = (RelativeLayout) view.findViewById(R.id.rl_nodata);
    }

    private void setlisteners() {
        refreshLayout.setOnRefreshListener(this);

        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(LayoutManager) {
            @Override
            public void onLoadMore() {
                page++;
                LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
                model.getData(page,"0", PartnerShouyi_DaifaFragment.this);
            }
        });
    }


    @Override
    public void onRefresh() {
        LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
        page = 1;
        model.getData(page,"0", this);
    }

    /**
     * @param object 获取收益记录成功
     */
    @Override
    public void ongetDataSuccess(Object object) {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }
        mBeans = (List<PartnerProfitBean>) object;
        if (page == 1) {
            mPartnerProfitBeans.clear();
        } else {
            if (mBeans.size() == 0) {
//                ToastUtils.makeToastShort("没有更多了");
                return;
            }
        }
        mPartnerProfitBeans.addAll(mBeans);

        if (mPartnerProfitBeans.size() == 0) {
            rlnodata.setVisibility(View.VISIBLE);
        } else {
            rlnodata.setVisibility(View.GONE);
        }
        showView();

    }

    private void showView() {
        if (mAdapter == null) {

            mAdapter = new CommonAdapter<PartnerProfitBean>(getActivity(), R.layout.item_daifa_partner, mPartnerProfitBeans) {
                @Override
                public void convert(ViewHolder holder, final PartnerProfitBean bean, int position) {
                    holder.setText(R.id.tv_ordernum, bean.getOrdersn());
                    holder.getTextView(R.id.tv_fuzhi).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ShareUtils.copyLianjie(getActivity(), bean.getOrdersn());
                        }
                    });
                    holder.setText(R.id.tv_title, bean.getTitle());
                    holder.setText(R.id.tv_profit, bean.getCpcome());
                    holder.setText(R.id.tv_time, bean.getOrdertime());
                    if (bean.getOrderstatus() == "1") {
                        holder.setText(R.id.tv_statu, "付款待发货");
                    } else if (bean.getOrderstatus() == "0") {
                        holder.setText(R.id.tv_statu, "下单未付款");
                    } else if (bean.getOrderstatus() == "2") {
                        holder.setText(R.id.tv_statu, "发货待收货");
                    } else if (bean.getOrderstatus() == "3") {
                        holder.setText(R.id.tv_statu, "已完成");
                    } else if (bean.getOrderstatus() == "-1"){
                        holder.setText(R.id.tv_statu, "关闭");
                    }else if (bean.getOrderstatus() == "-2"){
                        holder.setText(R.id.tv_statu, "关闭");

                    }
                }
            };
            recyclerView.setAdapter(mAdapter);
        }else {
            mAdapter.notifyDataSetChanged();
        }
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }

    }



    /**
     * @param object 获取收益记录失败
     */
    @Override
    public void ongetDataError(Object object) {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(getActivity(), jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }
}
